package com.gdth.widget.dwonloadlist;

import java.util.List;

import com.gdth.R;
import com.gdth.model.Item;
import com.gdth.utils.ImageUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class BaseDownListTextView extends LinearLayout{
	
	protected Context context;
	
	/**
	 * 输入框
	 */
	public TextView txt_value;
	
	/**
	 * 弹出框
	 */
	private PopupWindow pop;
	
	/**
	 * 下拉列表
	 */
	private ListView listView;
	
	/**
	 * 数据源适配器
	 */
	private DrawDownListAdapter popAdapter;  
	
	/**
	 * 数据源
	 */
	public List<Item> items;
	
	private Drawable drawable_up;
	private Drawable drawable_down;

	public String key, val;
	
	private int layout;
	
	public BaseDownListTextView(Context context) {
		super(context);
		this.context = context;
		this.layout = R.layout.widget_drawdownlist_textview;

		init();
		initView();
	}
	
	public BaseDownListTextView(Context context, int layout) {
		super(context);
		
		this.context = context;
		this.layout = layout;

		init();
		initView();
	}
	
	public BaseDownListTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		this.context = context;
		this.layout = R.layout.widget_drawdownlist_textview;
		
		init();
		initView();
	}
	
	public BaseDownListTextView(Context context, AttributeSet attrs, int layout) {
		super(context, attrs);
		
		this.context = context;
		this.layout = layout;

		init();
		initView();
	}
	
	private void init() {
		LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(layout, this);
        
		drawable_up = ImageUtil.getImageAsDrawable(context, R.drawable.drawdownlist_btn_up);
		drawable_down = ImageUtil.getImageAsDrawable(context, R.drawable.drawdownlist_btn_down);
    }
	
	public void initView(){
        txt_value = (TextView) findViewById(R.id.widget_drawdownlist_textview_value);
        txt_value.setOnClickListener(new clickListener());
	}
	
	private class clickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			setUpIbtnListeners();
		}
		
	}
	
    /**
     * 生成弹出框
     */
	public void setUpIbtnListeners() {
		if (pop == null) {  
			if (popAdapter == null && items != null && items.size() > 0) { 
				popAdapter = new DrawDownListAdapter(this.context, this.items, touchListener);  
	
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(20,20,20,20);
				listView = new ListView(this.context);
				if (listView_Theme != 0) {
					listView.setBackgroundResource(listView_Theme);
				} else {
					listView.setBackgroundResource(R.drawable.background_corner_padding);
				}
				
				listView.setLayoutParams(layoutParams);
				
				listView.setAdapter(popAdapter); 
				
				pop = new PopupWindow(listView, this.getWidth(), LayoutParams.WRAP_CONTENT);  
				
				pop.showAsDropDown(this);  
				popAdapter.isShow = true;
				
				txt_value.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable_up, null);
			}  
		} else if (popAdapter.isShow) {  
			popupWindowHide();
		} else if (!popAdapter.isShow) {  
			pop.showAsDropDown(this);  
			popAdapter.isShow = true;  
			
			txt_value.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable_up, null);
		}   
	}

	/**
	 * 列表点击事件
	 */
	OnClickListener touchListener = new OnClickListener() {  
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			TextView text = (TextView)arg0;
			
			key = text.getTag().toString();
			val = text.getText().toString();
            
			txt_value.setText(val);
			txt_value.setTag(key);
            
			popupWindowHide();
		}  
    };
    
    private void popupWindowHide(){
    	popAdapter.isShow = false; 
        pop.dismiss(); 
        
		txt_value.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable_down, null);
    }
    
    int listView_Theme;
    public void setTheme(int listView_Theme) {
    	this.listView_Theme = listView_Theme;
    }
	
	public void setItem(List<Item> items){
		this.items = items;
	}
	
	public Object getKey() {
		return this.txt_value.getTag();
	}
	
	public String getValue() {
		return this.txt_value.getText().toString();
	}
	
	public void setValue(String key){
		if (items == null) {
			txt_value.setText(key);
			txt_value.setTag(key);
			return;
		}
		for(Item mItem : items){
			if (mItem.getKey().equals(key)){
				txt_value.setText(mItem.getValue());
				txt_value.setTag(mItem.getKey());
				this.key = mItem.getKey();
				this.val = mItem.getValue();
			}
		}
	}
	
	/*********************定义数据返回接口***********************/
	public OnSelectListener selectListener;
	public void setOnSelectListener(OnSelectListener selectListener){
		this.selectListener = selectListener;
	}
	
	/**
	 * 数据返回接口
	 * @author youyuan
	 *
	 */
	public interface OnSelectListener {  
        /** 
         * 向左滑动子View 
         */  
        public abstract void onchange(String key, String val); 
    }
}
